use serde::de::DeserializeOwned;
use serde_yml;
use std::fmt::Debug;

pub fn read_yaml_file<T>(file_path: &str) -> T where T: DeserializeOwned {
  let read_err = format!("cannot read file to string[{}]", file_path);
  let file_content = std::fs::read_to_string(file_path).expect(read_err.as_str());
  let parse_err = format!("cannot treat file content as yaml: [{}]", file_content.clone());
  let file_info = serde_yml::from_str::<T>(file_content.as_str()).expect(parse_err.as_str());
  file_info
}

pub fn yaml_stringify<T: serde::Serialize + Debug>(raw_info: &T) -> String {
  serde_yml::to_string(raw_info).expect(
    format!("cannot convert yaml to string, {:#?}", raw_info).as_str()
  )
}

pub fn write_yaml_file<T: serde::Serialize + Debug>(file_path: &str, raw_info: &T) {
  let file_content = yaml_stringify(raw_info);
  std::fs::write(file_path, file_content.as_bytes()).expect(
    format!("cannot write file to string[{}]", file_path).as_str()
  );
}
